#include "../includes.h"

LOG_	LOG;
TLOG_	TLOG;

int child_calling_exit = 0;

LOG_DATA_  LogQueue[LOG_QUEUE_LENGTH];	/* Buffer for transmit chars. */
LOG_DATA_* LogQueue_In;					/* Next empty position. */
LOG_DATA_* LogQueue_Out;				/* Position of last stored char. */

LOG_CATA_STR_ CAT1[LOG_CATA1_MAX] = {
	{LOG_CATA1_NORM, 		"NORM"},
	{LOG_CATA1_SPEC, 		"SPEC"},
	{LOG_CATA1_WARN, 		"WARN"}
};

LOG_CATA_STR_ CAT2[LOG_CATA2_MAX] = {
	{LOG_CATA2_NOTHING,		"       "},
	{LOG_CATA2_OK,			"[ OK ] "},
	{LOG_CATA2_FAIL,		"[FAIL] "},
	{LOG_CATA2_WARN,		"[WARN] "},
	{LOG_CATA2_INFO,		"[INFO] "},
	{LOG_CATA2_IMPORTANT1,	"[ *  ] "},
	{LOG_CATA2_IMPORTANT2,	"[ ** ] "},
	{LOG_CATA2_PLUS_ME,		"[ M+ ] "},
	{LOG_CATA2_PLUS_YOU,	"[ U+ ] "},
	{LOG_CATA2_MINUS_ME,	"[ M- ] "},
	{LOG_CATA2_MINUS_YOU,	"[ U- ] "}
};


int Setting_LOG();


void PresetLOG();
void PresetTLOG();

int found_log, first_log, last_log, acc_log;
char LogStartFlag;
char Setting_LOG_Flag;


void Init_LOG(void)
{
	LOG.LogMessageEvent = OFF;
	LOG.LogPresetEvent = OFF;

	LOG.LogMessageNo = 0;

	LOG.DISPLAY_NO = 0;
	LOG.Precise_Mode = OFF;

	LOG.CNT = 0;
	//LOG.TimeOut = 0;
	LOG.LogCounter = 0;
	LOG.LastLogCounter = 0;
	
    LOG.FirstLog = 0;
    LOG.LastLog = -1;
	LOG.AccLogCounter = -1;

	LOG.Page_Mode = ON;

//system("rm -f /gpsclock/GDULog*");
//system("rm -f /gpsclock/GDULog2");
//system("mkdir -p /gpsclock/INV/");
	//kang PresetTLOG();

	//kang LogQueue_In = LogQueue_Out = LogQueue;

	//kang LogStartFlag = OFF;
	//kang Setting_LOG_Flag = OFF;

}


char ConfigLogEvent(void)
{
	LOG.TimeOut++;

	if ( CONFIG.WriteEvent == ON ){
		CONFIG.WriteEvent = OFF;
		WriteCONFIG();
		backup_copy_config();
	}
	else{

		if( LOG.CNT >= 10 && LOG.TimeOut >= 30 ){     //30sec
			backup_copy_log();
			
			LOG.CNT = 0;
			LOG.TimeOut = 0;
		}
		else if( LOG.CNT < 10 && LOG.TimeOut >= 120 ){ //2min
			backup_copy_log();
			
			LOG.CNT = 0;
			LOG.TimeOut = 0;
		}
		else if( LOG.CNT == 0 && LOG.TimeOut >= 600 ){ //10min
			LOG.CNT = 0;
			LOG.TimeOut = 0;
		}
		else{
		
		} 
	}
	
/*kang	
	else if ( LOG.LogMessageEvent == ON ){
		LOG.LogMessageEvent = OFF;
		WriteLOG(LOG_NORMAL);
	}
	else if ( LOG.LogPresetEvent == ON ){
		LOG.LogPresetEvent = OFF;
		WriteLOG(LOG_PRESET);
	}
kang*/
	return 0;
}


void PresetLOG()
{
    LOG.LogCounter = 0;
    LOG.FirstLog = 0;
    LOG.LastLog = -1;
	LOG.AccLogCounter = -1;

    LOG.LogPresetEvent = ON;
}








extern inline int set_lowest_sched(void);
void backup_copy_config(void)
{
char cmd[512];

	switch (fork()) {
		case -1:
			LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "bakcup_config fork");
	   		break;
	
		case 0:
			set_lowest_sched();
	
			usleep(10000);
	
			if (setsid()<0) {
				LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "bakcup_config setsid");
				child_calling_exit = 1;
				exit(EXIT_SUCCESS);
			}
			
			sprintf(cmd,"cp -f /app/config /gpsclock/config");
			
			usleep(10000);
			
			child_calling_exit = 1;
			
			execl("/bin/sh", "sh", "-c", cmd, NULL);
			exit(EXIT_SUCCESS);
			break;
		
		default:
			//fwup_do_fork(state, reason);
			break;
	}	
	
}


void backup_copy_log(void)
{
struct stat st;
char cmd[512];

	if(LOG.CNT<=0){return;}
		
	stat(GDU_LOG_NEW_PATH, &st);
	
	switch (fork()) {
		case -1:
			LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "bakcup_log fork");
	   		break;
	
		case 0:
			set_lowest_sched();
	
			usleep(10000);
	
			if (setsid()<0) {
				LogItString(LOG_CATA1_NORM, LOG_CATA2_FAIL, "bakcup_log setsid");
				child_calling_exit = 1;
				exit(EXIT_SUCCESS);
			}
			
			if(st.st_size>LOG_FILE_MAX_SIZE){
				sprintf(cmd,
					"cp -f %s %s &&"
					"cat %s > %s &&"
					"rm -f %s",
					GDU_LOG_NEW_PATH,GDU_LOG_OLD_PATH,
					NameRAMLogFile, GDU_LOG_NEW_PATH,
					NameRAMLogFile
				);
			}
			else{
				sprintf(cmd,
					"cat %s >> %s &&"
					"rm -f %s",
					NameRAMLogFile, GDU_LOG_NEW_PATH,
					NameRAMLogFile
				);
			}
			
			usleep(10000);
			
			child_calling_exit = 1;
			
			execl("/bin/sh", "sh", "-c", cmd, NULL);
			exit(EXIT_SUCCESS);
			break;
		
		default:
			//fwup_do_fork(state, reason);
			break;
	}
	
}


int Setting_LOG(void)
{
	char result;
	
	return 0; //kang
	
    PresetLOG();
	
    //kang result = ReadLOG();  /* read data + checksum */

 	if( (result == FAIL) || (LOG.LogCounter < 0) ){
		PresetLOG();
		MessageOut("LOG Cleared\n");
	}
	else{

        if(SYSTEM.IO.CTRL_PORT.MODE == UDEBUG){
            sprintf((char*)MESG,"LOG :%d\n",LOG.LogCounter);
            TransmitMessage((BYTE*)MESG,DEBUG_PORT);
        }
	}
    return YES;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void PresetTLOG()
{
    TLOG.LogCounter = 0;
    TLOG.FirstLog = 0;
    TLOG.LastLog = 0;
    TLOG.AccLogCounter = 0;
/*
	for( i = 0; i < MAX_TLOG; i++) {
		TLOG.Message[i].ID 			= 0;
		TLOG.Message[i].YEAR		= 0;
		TLOG.Message[i].MONTH		= 0;
		TLOG.Message[i].DAY			= 0;
		TLOG.Message[i].HOUR		= 0;
		TLOG.Message[i].MINUTE		= 0;
		TLOG.Message[i].SECOND		= 0;
		TLOG.Message[i].PARAMETER	= 0;
	}
*/

}


void TLogIt(int name, int parameter)
{
	return;//kang

    TLOG.Message[TLOG.LastLog].ID = name;

    TLOG.Message[TLOG.LastLog].YEAR    = SYSTEM.TIME.CUR.YEAR;
    TLOG.Message[TLOG.LastLog].MONTH   = SYSTEM.TIME.CUR.MONTH;
    TLOG.Message[TLOG.LastLog].DAY     = SYSTEM.TIME.CUR.DAY;
    TLOG.Message[TLOG.LastLog].HOUR    = SYSTEM.TIME.CUR.HOUR;
    TLOG.Message[TLOG.LastLog].MINUTE  = SYSTEM.TIME.CUR.MINUTE;
    TLOG.Message[TLOG.LastLog].SECOND  = SYSTEM.TIME.CUR.SECOND;
    TLOG.Message[TLOG.LastLog].MSECOND = get_msec();

	TLOG.Message[TLOG.LastLog].PARAMETER1 = parameter;

	// LastLog.
	TLOG.LastLog = (TLOG.LastLog+1)%MAX_TLOG;
	// FirstLog.
	if( TLOG.LogCounter == MAX_TLOG ) {
		TLOG.FirstLog = (TLOG.FirstLog+1)%MAX_TLOG;;
	}

	//----------------------------------------
	// Counter
	//----------------------------------------
	if (TLOG.LogCounter != MAX_TLOG){
		TLOG.LogCounter++;
	}

	// AccLogCounter.
	TLOG.AccLogCounter++;
}


void Display1TLog(int DisplayNo)
{
	int		index;
	
	LOG_DATA_	pLogMessage;
	char	msg[300];
	char	parameter[150];

	return;//kang
	
	index = (TLOG.FirstLog+DisplayNo)%MAX_TLOG;

	pLogMessage = TLOG.Message[index];
	
	sprintf(msg,"%3d> %04d/%02d/%02d %02d:%02d:%02d.%03d ",
				DisplayNo+1,
				pLogMessage.YEAR, pLogMessage.MONTH, pLogMessage.DAY, // ð
				pLogMessage.HOUR, pLogMessage.MINUTE, pLogMessage.SECOND,
				pLogMessage.MSECOND );  // Parameter�?

	switch ( pLogMessage.ID ) {

    	case TLOG_Fault: 	strcat(msg,"LOG: ");
    						sprintf(parameter,"[%d]\n",pLogMessage.PARAMETER1);
    						strcat(msg,parameter);
    						break;

    	case TLOG_dDAC_Fail:strcat(msg,"dDAC fail: ");
    						sprintf(parameter,"[%d]\n",pLogMessage.PARAMETER1);
    						strcat(msg,parameter);
    						break;

    	case LOG_OCXO_Error: 	strcat(msg,"Fault: OCXO\n");		break;


		case TLog_No_Sat:	strcat(msg,"No Sat - ");
    						sprintf(parameter,"[%d]\n",pLogMessage.PARAMETER1);
    						strcat(msg,parameter);
	   						break;
		case TLOG_YEAR_ERR:	strcat(msg,"Year Error: ");
    						sprintf(parameter,"[%d]\n",pLogMessage.PARAMETER1);
    						strcat(msg,parameter);
    						break;	
    	case TLOG_FUNC_FAIL: strcat(msg,"Function Fail: ");
    						sprintf(parameter,"[%d]\n",pLogMessage.PARAMETER1);
    						strcat(msg,parameter);
    						break;	
    	case TLOG_TOD_BEFORE:
    						sprintf(parameter,"TOD Before: [%d %d:%d:%d]\n", 
    							pLogMessage.PARAMETER1>>24&0x000000FF,
    							pLogMessage.PARAMETER1>>16&0x000000FF,
    							pLogMessage.PARAMETER1>>8&0x000000FF,
    							pLogMessage.PARAMETER1&0x000000FF
    							);
    						strcat(msg,parameter);
    						break;
		case TLOG_TOD_AFTER:
							sprintf(parameter,"TOD After: [%d %d:%d:%d]\n", 
    							pLogMessage.PARAMETER1>>24&0x000000FF,
    							pLogMessage.PARAMETER1>>16&0x000000FF,
    							pLogMessage.PARAMETER1>>8&0x000000FF,
    							pLogMessage.PARAMETER1&0x000000FF
    							);
    						strcat(msg,parameter);
    						break;    
    	case TLOG_TRAIM_ALM:
    						sprintf(parameter,"TRAIM Alarm: [%d, %d, %d]\n", 
    							pLogMessage.PARAMETER1>>16&0x0000FFFF,
    							pLogMessage.PARAMETER1>>8&0x000000FF,
    							pLogMessage.PARAMETER1&0x000000FF
    							);
    						strcat(msg,parameter);
    						break;
    	case TLOG_TRAIM_UNKNOWN:
    						sprintf(parameter,"TRAIM Unknown: [%d, %d, %d]\n", 
    							pLogMessage.PARAMETER1>>16&0x0000FFFF,
    							pLogMessage.PARAMETER1>>8&0x000000FF,
    							pLogMessage.PARAMETER1&0x000000FF
    							);
    						strcat(msg,parameter);
    						break;
    	case TLOG_TRAIM_OK:
    						sprintf(parameter,"TRAIM Ok: [%d, %d, %d]\n", 
    							pLogMessage.PARAMETER1>>16&0x0000FFFF,
    							pLogMessage.PARAMETER1>>8&0x000000FF,
    							pLogMessage.PARAMETER1&0x000000FF
    							);
    						strcat(msg,parameter);
    						break;
    	case TLOG_TRAIM_ERROR:
    						sprintf(parameter,"TRAIM Error: [%d, %d, %d]\n", 
    							pLogMessage.PARAMETER1>>16&0x0000FFFF,
    							pLogMessage.PARAMETER1>>8&0x000000FF,
    							pLogMessage.PARAMETER1&0x000000FF
    							);
    						strcat(msg,parameter);
    						break;
    	default:
    						sprintf(parameter,"%d: [%d]\n", pLogMessage.ID, pLogMessage.PARAMETER1);
    						strcat(msg,parameter);
    						break;
	}

	TransmitMessage((BYTE*)msg,DEBUG_PORT);
}

char DisplayTLog(void)
{
	return 0; //kang

	while(TLOG.DISPLAY_NO < TLOG.LogCounter) {


		Display1TLog(TLOG.DISPLAY_NO++);

		if ( TLOG.DISPLAY_NO == TLOG.LogCounter ) {
			SYSTEM.IO.CTRL_PORT.MODE = UWAIT_TO_COMMAND;
			break;
		}
		else if ( !(TLOG.DISPLAY_NO % 20) ) {
			TransmitMessage((BYTE*)" === Press any key to continue or '/' to quit ===\n",DEBUG_PORT);
			// Wait mode��ȯ.
			SYSTEM.IO.CTRL_PORT.MODE = UWAIT_TLOG;
			break;
		}
	}
	
	//SYSTEM.IO.CTRL_PORT.MODE = UWAIT_TO_COMMAND;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


void LogIt(int cata1, int cata2, int name, int parameter)
{
	LogIt3(cata1, cata2, name, parameter, 0, 0);
}

int prevLogNumber=-1;
unsigned int prevLogTimeOut=0;
char msg_to_write[1100];
void LogWirteToFile(int name, char *s)
{
	int fdRLOG;

	if ( name!=-1 && name == prevLogNumber && prevLogTimeOut < 4 ){ return ;}
	else prevLogTimeOut = 0;
	prevLogNumber = name;
	//fdRLOG = open( NameRAMLogFile, O_RDWR | O_APPEND | O_CREAT);

	fdRLOG = open( NameRAMLogFile, (O_WRONLY|O_CREAT|O_APPEND|O_NONBLOCK) );
	if ( fdRLOG < 0 ){
		printf( "Open Error Log File in the RAM...:%s\n", NameRAMLogFile );
		return;
	}

	write(fdRLOG, msg_to_write, strlen(msg_to_write));
	close(fdRLOG);
	//printf("%s >> %s\n",NameRAMLogFile,msg_to_write);
}

void LogWirteToFile2nd(char *s)
{
	LogWirteToFile(-1,s);
}

void LogIt3(int cata1, int cata2, int name, int parameter1, int parameter2, int parameter3)
{
char msg[600], msg2[300];
char parameter[300];

int DisplayIndex;
int log_count, log_block;


	LOG.CNT++;
	LOG.LogCounter++;
	
	//msg[0]=0;
	strcpy(msg,"");
	switch( name ){

    	//case LOG_Start:			strcat(msg,"######Started######");	//break;
    	//							sprintf(parameter,"[%d]",pLogMessage.PARAMETER1);
    	//							strcat(msg,parameter);				break;
    	case LOG_Start:			sprintf(parameter,"######Started###### [%d, %d, %x]",
    									parameter1, parameter2, parameter3);
									strcat(msg, parameter);				break;
									
    	//case LOG_RESET:		sprintf(parameter,"Ready to Start",parameter1);
    	//							strcat(msg,parameter);				break;
		case LOG_RESET:			sprintf(parameter,"===Ready to Start [%d] 0:Cold, 1:Warm",parameter1);
									strcat(msg,parameter);					break;
									
    	case LOG_Set_To_Default:strcat(msg,"Set to Default");			break;
		
    	case LOG_Lock:      		strcat(msg,"Locked to Reference");		break;
		
    	case LOG_Holdover:  		strcat(msg,"Holdover");					break;
		
    	//case LOG_Active:    	strcat(msg,"Active");				break;
    	case LOG_Active:    		//sprintf(parameter,"Active [%d, %d, %d, %d]",
    								//	(parameter1>>24)&0x000000FF,
    								//	parameter1&0x00FFFFFF,
    								//	parameter2, 
    								//	parameter3);
									//strcat(msg,parameter);					break;
									strcat(msg,"Active");					break;
    	case LOG_Standby:   		strcat(msg,"Standby");					break;
		
    	case LOG_Block:     		strcat(msg,"Blocked");					break;
		
    	//case LOG_1PPS_Alarm:	strcat(msg,"1PPS Alarm");			break;
    	//case LOG_1PPS_Alarm:	sprintf(parameter,"1PPS Alarm [ro:%d ns, avo:%d ns, dac:%d, sat:%d]",
    	//								parameter1*100, 
    	//								parameter2*100,
    	//								(unsigned short)((parameter3>>16)&0xFFFF),
    	//								(short)((parameter3>>8)&0xFF)
    	//							);
		//							strcat(msg,parameter);					break;
		//							
    	//case LOG_1PPS_Ok:		sprintf(parameter,"1PPS Ok [ro:%d ns, avo:%d ns, dac:%d, sat:%d]",
    	//								parameter1*100,
    	//								parameter2*100,
    	//								(int)((parameter3>>16)&0xFFFF),
    	//								(int)((parameter3>>8)&0xFF)
    	//							);
		//							strcat(msg,parameter);					break;
		case LOG_1PPS_Alarm:	
		case LOG_1PPS_Ok:
								sprintf(parameter,"1PPS Alarm, ro:%dns, avo:%dns, dac:%d, sat:%d]",
									parameter1*100, 
    								parameter2*100,
    								(unsigned short)((parameter3>>16)&0xFFFF),
    								(short)((parameter3>>8)&0xFF)
    							);
								strcat(msg,parameter);					break;
    	//case LOG_EFC_ERROR:	strcat(msg,"EFC Error");				break;
    	//case LOG_EFC_ERROR:	sprintf(parameter,"EFC Error [dac:%d, TDAC:%d, avo:%d ns, timeoffset:%d ns, sat:%d, temp:%d]",
    	//								(unsigned short)((parameter1>>16)&0xFFFF),
    	//								(unsigned short)(parameter1&0xFFFF),
    	//								parameter2*100,
    	//								(int)((parameter3>>16)&0xFFFF)*100,
    	//								(short)((parameter3>>8)&0xFF),
    	//								(short)(parameter3&0xFF)
    	//							);
    	//							strcat(msg,parameter);					break;
		//							
    	//case LOG_EFC_OK: 		sprintf(parameter,"EFC Ok [dac:%d, TDAC:%d, avo:%d ns, timeoffset:%d, sat:%d, temp:%d]",
    	//								(unsigned short)((parameter1>>16)&0xFFFF),
    	//								(short)(parameter1&0xFFFF),
    	//								parameter2*100,
    	//								(int)((parameter3>>16)&0xFFFF)*100,
    	//								(short)((parameter3>>8)&0xFF),
    	//								(short)(parameter3&0xFF)
    	//							);
    	//							strcat(msg,parameter);					break;
    	case LOG_EFC_ERROR:	
    	case LOG_EFC_OK:
    							sprintf(parameter,"EFC, dac:%d, TDAC:%d, avo:%d ns, toff:%dns, sat:%d, temp:%d]",
									(unsigned short)((parameter1>>16)&0xFFFF),
    								(short)(parameter1&0xFFFF),
    								parameter2*100,
    								(int)((parameter3>>16)&0xFFFF)*100,
    								(short)((parameter3>>8)&0xFF),
    								(short)(parameter3&0xFF)
    							);
    							strcat(msg,parameter);					break;
    							
    	case LOG_Fault:   			strcat(msg,"SYSTEM Fault");			break;
		
    	case LOG_EB_Error: 		strcat(msg,"Fault receiver");			break;
		
    	case LOG_OCXO_Error: 	strcat(msg,"Fault OCXO");				break;
		
    	//case LOG_RB_Error: 	strcat(msg,"Fault: Rb.");				break;

		case LOG_RB_TX_Error:		strcat(msg,"Fault: Rb TX.");		break;
		
    	case LOG_RB_LOCK_Error:	strcat(msg,"Fault: Rb Lock.");		break;

    	// parameter 0: run(increase) error 1: start error
    	case LOG_RTC_Error:		sprintf(msg2,"Fault: RTC [%d]", parameter1);
    								strcat(msg,msg2); 						break;

    	case LOG_HW_FAIL: 		strcat(msg,"Fault: HW Fail");
    								sprintf(parameter,"[%x]", parameter1);
    								strcat(msg,parameter);					break;
									
    	case LOG_DEBUG:			sprintf(parameter,"[%d]", parameter1);
    								strcat(msg,parameter);					break;
									
    	case LOG_UPDATE_DAC:	sprintf(msg2,"DAC updated [%d -> %d, %d]",
    									parameter1, parameter2, parameter3);
    								strcat(msg,msg2); 						break;

    	case LOG_SIG_ON: 		strcat(msg,"Signal On");				break;
		
    	case LOG_SIG_OFF: 		strcat(msg,"Signal Off");		    	break;
		
    	case LOG_10MHz:			strcat(msg,"10MHz Fail");    			break;
		
    	//case LOG_dDAC_FAIL:	strcat(msg,"dDAC Fail");    		break;	
		//case LOG_dDAC_FAIL:		sprintf(msg2,"dDAC Fail [ro:%d ns, avo:%d ns, dac:%d, mdac:%d, timeoffset:%d]",
		//								parameter1*100,
		//								(int)((parameter3>>16)&0xFFFF)*100,
		//								(unsigned short)((parameter2>>16)&0xFFFF),
    	//								(unsigned short)(parameter2&0xFFFF),
    	//								(short)(parameter3&0xFFFF)
		//							);
    	//							strcat(msg,msg2);						break;
		//							
		//case LOG_dDAC_OK:		sprintf(msg2,"dDAC Ok [ro:%d ns, avo:%d ns, dac:%d, mdac:%d, timeoffset:%d]",
		//								parameter1*100,
		//								(int)((parameter3>>16)&0xFFFF)*100,
		//								(unsigned short)((parameter2>>16)&0xFFFF),
    	//								(unsigned short)(parameter2&0xFFFF),
    	//								(short)(parameter3&0xFFFF)
		//							);
    	//							strcat(msg,msg2);						break;
    	case LOG_dDAC_FAIL:	
    	case LOG_dDAC_OK:	
    							sprintf(msg2,"dDAC, ro:%dns, avo:%dns, dac:%d, mdac:%d, toff:%d]",
    								parameter1*100,
									(int)((parameter3>>16)&0xFFFF)*100,
									(unsigned short)((parameter2>>16)&0xFFFF),
    								(unsigned short)(parameter2&0xFFFF),
    								(short)(parameter3&0xFFFF)
								);
    							strcat(msg,msg2);						break;
    	
		//case LOG_Receiver_Alarm:	strcat(msg,"1PPS offline ");
    	//								sprintf(parameter,"[%d %d]",
    	//									(short)((parameter1>>16)&0xFFFF),
    	//									(short)(parameter1&0xFFFF));
    	//								strcat(msg,parameter);				break;
		
		//case LOG_Receiver_Good:	strcat(msg,"1PPS online ");
    	//								sprintf(parameter,"[%d %d]",
    	//									(short)((parameter1>>16)&0xFFFF),
    	//									(short)(parameter1&0xFFFF));
    	//								strcat(msg,parameter);				break;

		case LOG_Receiver_Alarm:		sprintf(msg2,"1PPS offline [PrevOffset:%d ns -> %d ns, dac:%d, TDAC:%d, ulockCnt:%d]",
    										(int)((parameter1>>16)&0xFFFF),
    										(int)(parameter1&0xFFFF),
											(unsigned short)((parameter2>>16)&0xFFFF),
    										(short)(parameter2&0xFFFF),
    										(unsigned int)parameter3
										);
    									strcat(msg,msg2);					break;
										
		case LOG_Receiver_Good:	sprintf(msg2,"1PPS online [Offset:%d ns,ref:%d ns, dac:%d, tdac:%d, ulockCnt:%d]",
    									(int)(parameter1&0xFFFF),
    									(int)((parameter1>>16)&0xFFFF),
										(unsigned short)((parameter2>>16)&0xFFFF),
    									(short)(parameter2&0xFFFF),
										(unsigned int)parameter3
									);
    								strcat(msg,msg2);						break;
    							
		case LOG_TOD_Correction:	sprintf(parameter,"TOD correction[%d]", (short)(parameter1));
    									strcat(msg,parameter);				break;


    	//case LOG_POWER_FAIL:	strcat(msg,"Fail: power");				break;
    	case LOG_POWER_OK:
    	case LOG_POWER_FAIL:	sprintf(msg2,"Power, 5v:%.2f, vcc:%.2f, vee:%.2f",
    									(float)((float)parameter1/100.0),
    									(float)((float)parameter2/100.0),
    									(float)((float)parameter3/100.0)
								);
    							strcat(msg,msg2);						break;
    	//case LOG_POWER_OK:		strcat(msg,"Ok: power");				break;


    	case LOG_EB_In:			strcat(msg,"GPS receiver in");		break;
    	case LOG_EB_Out: 		strcat(msg,"GPS receiver out");	break;

    	case LOG_Antenna_Open:	strcat(msg,"GPS antenna open");		break;
    	case LOG_Antenna_Short:	strcat(msg,"GPS antenna short");		break;
		case LOG_Antenna_OK:		strcat(msg,"GPS antenna ok");			break;
			
    	case LOG_GPS_Lock:		strcat(msg,"GPS lock");				break;
    	case LOG_GPS_Unlock: 	strcat(msg,"GPS unlock");				break;
    	
    	case LOG_EB_Unlock:		strcat(msg,"GPS receiver unlock");			break;
		case LOG_EB_Lock: 		strcat(msg,"GPS receiver lock");			break;

    	case LOG_E1_In:			strcat(msg,"E1/8K - In");				break;
    	case LOG_E1_Out: 		strcat(msg,"E1/8K - Out");			break;

    	case LOG_E2_In: 			strcat(msg,"E1/8K-2 - In");			break;
    	case LOG_E2_Out: 		strcat(msg,"E1/8K-2 - Out");			break;

    	case LOG_LMU_In: 		strcat(msg,"LMU - In");				break;
    	case LOG_LMU_Out: 		strcat(msg,"LMU - Out");				break;


    	case LOG_TRACE:			sprintf(parameter,"trace: [%d]", parameter1);
    								strcat(msg,parameter);					break;


		//-------------------------------------------------------------------------------
		//2004-02-17 4:58
		//switch Event
		case LOG_SWITCH_B2AS:	sprintf(parameter,"Switch[B2AS]:%08x,%08x,%d",
										parameter1,	parameter2,	parameter3);
									strcat(msg,parameter);					break;
		case LOG_SWITCH_PS2S:	sprintf(parameter,"Switch[PS2S]:%08x,%08x,%d",
										parameter1,	parameter2,	parameter3);
									strcat(msg,parameter);					break;
		case LOG_SWITCH_A2B:	sprintf(parameter,"Switch[A2B]:%08x,%08x,%d",
										parameter1,	parameter2,	parameter3);
									strcat(msg,parameter);					break;
		case LOG_SWITCH_S2B:	sprintf(parameter,"Switch[S2B]:%08x,%08x,%d",
										parameter1,	parameter2,	parameter3);
									strcat(msg,parameter);					break;
		case LOG_SWITCH_A2S:	sprintf(parameter,"Switch[A2S]:%08x,%08x,%d",
										parameter1,	parameter2,	parameter3);
									strcat(msg,parameter);					break;
		case LOG_SWITCH_S2A:	sprintf(parameter,"Switch[S2A]:%08x,%08x,%d",
										parameter1,	parameter2,	parameter3);
									strcat(msg,parameter);					break;
		case LOG_SWITCH_PS2B:	sprintf(parameter,"Switch[PS2B]:%08x,%08x,%d",
										parameter1,	parameter2,	parameter3);
									strcat(msg,parameter);					break;
		//-------------------------------------------------------------------------------

		case LOG_DUAL_DATA_NOT_RECEIVE:	strcat(msg,"Dual Data Not Receive!!");	break;

		case LOG_CONTROL_SYNC_TYPE:			sprintf(parameter,"Control sync type %d", parameter1);
													strcat(msg,parameter);					break;
													
		//case LOG_CONTROL_SYNC:				sprintf(parameter,"Control sync %d", parameter1);
		//											strcat(msg,parameter);					break;
		case LOG_CONTROL_SYNC:				sprintf(parameter,"Control re-sync, type,%d, o,%d ns", 
														parameter1, parameter2*100);
													strcat(msg,parameter);					break;
													
		case LOG_TRACE_SYNC:					sprintf(parameter,"Trace re-sync, %d", parameter1);
													strcat(msg,parameter);					break;
													
		case LOG_EVEN_SYNC:						sprintf(parameter,"Even sync [%d]",	parameter1);
													strcat(msg,parameter);					break;

    	case LOG_EB_STATUS:	sprintf(msg2,"GPS status, v:%02d, t:%02d, d:%02d, "
    											"S/S:"
												"%02d,%02d,%02d,"
												"%02d,%02d,%02d,"
												"%02d,%02d, temp:%02d",
    											parameter1&0xFF,parameter2&0xFF,parameter3&0xFF,
        										(parameter1>>24)&0xFF,
        										(parameter1>>16)&0xFF,
        										(parameter1>>8)&0xFF,
        										
        										(parameter2>>24)&0xFF,
        										(parameter2>>16)&0xFF,
        										(parameter2>>8)&0xFF,
        										
        										(parameter3>>24)&0xFF,
        										(parameter3>>16)&0xFF,
        										(parameter3>>8)&0xFF
    								);
    								strcat(msg,msg2); 										break;
									
		case LOG_SYNC_ERR:		sprintf(parameter,"Even sync Error [%d]",	parameter1);
									strcat(msg,parameter);									break;
									
		case LOG_RESET_ERR1:	sprintf(parameter,"[Reset Value1] %08X %08X %08X",
										parameter1, parameter2, parameter3);
									strcat(msg,parameter);									break;
									    
		case LOG_RESET_ERR2:	sprintf(parameter,"[Reset Value2] %08X %08X %08X",
										parameter1, parameter2, parameter3);
									strcat(msg,parameter);									break;
									   
		case LOG_RESET_ERR3:	sprintf(parameter,"[Reset Value3] %08X %08X %08X",
										parameter1, parameter2, parameter3);
									strcat(msg,parameter);									break;
									   
		case LOG_1PPS_LOST:		sprintf(parameter,"GPS Receiver 1PPS Lost [%d]",parameter1);	
									strcat(msg,parameter);									break;
									
		case LOG_1PPS_VAL:		sprintf(parameter,"GPS Receiver 1PPS Value [%08X %08X %08X]",
										parameter1, parameter2, parameter3);	
									strcat(msg,parameter);									break;
									
		case LOG_1PPS_RESET:	sprintf(parameter,"GPS Receiver 1PPS Reset [%d]", parameter1);	
									strcat(msg,parameter);									break;
									
		case LOG_1PPS_SET:		sprintf(parameter,"GPS Receiver 1PPS Set [%d]", parameter1);	
									strcat(msg,parameter);									break;
									
		case LOG_WDT_OVER:		sprintf(parameter,"Watchdog overtime [%d, %d]",
										parameter1, parameter2);	
									strcat(msg,parameter);									break;
									
		case LOG_SYNC15_RESET:	sprintf(msg2,"GPS receiver restart: %d, %d, %d",
										parameter1, parameter2, parameter3);
    								strcat(msg,msg2);										break;
									
		case LOG_OFF_RPT1:		sprintf(msg2,"Offset report, ro:%dns, prevRO:%dns, dac:%d, sat:%d, temp:%d",
    									parameter1*100,parameter2*100,
    									(unsigned short)((parameter3>>16)&0xFFFF),
    									(short)((parameter3>>8)&0xFF),
    									(short)(parameter3&0xFF));
    								strcat(msg,msg2);										break;
									
		case LOG_SYNC15_FREQ:	sprintf(parameter,"GPS receiver frequency [%d, %d, %d ns, %d E-11]",
										(parameter1>>16)&0xFFFF,
        								(parameter1)&0xFFFF,
										parameter2*100, 
										parameter3);	
									strcat(msg,parameter);									break;
									
		case LOG_TFOM:			sprintf(parameter,"Over TFOM [%d]", parameter1);
									strcat(msg,parameter);									break;
									
		case LOG_COAST:			sprintf(parameter,"Coasting [%d]", parameter1);
									strcat(msg,parameter);									break;
									
		case LOG_RECOVERY:		sprintf(parameter,"Recovery [%d]", parameter1);	
									strcat(msg,parameter);									break;
									
		case LOG_POS_HOLD1:		sprintf(parameter,"Position hold1 [%d,%d %d,%d %d]",
										(parameter1>>16) & 0x0000FFFF,
										parameter1 & 0x0000FFFF,
										(parameter2>>16) & 0x0000FFFF,
										parameter2 & 0x0000FFFF,
										parameter3
									);	
									strcat(msg,parameter);									break;
									
		case LOG_POS_HOLD2:		sprintf(parameter,"Position hold2 [%6.3f, %6.3f]",
										(float)(parameter1)/1000.0,
										(float)(parameter2)/1000.0
									);
									strcat(msg,parameter);									break;
									
		case LOG_RECOVERY_END:	sprintf(parameter,"Recovery offset [%d ns]",parameter1*100);
										strcat(msg,parameter);								break;
										
		case LOG_39M_CLEAR:		sprintf(parameter,"39M Clear [%d, %d]",
											parameter1, parameter2);
										strcat(msg,parameter);								break;
										
		case LOG_SYNC15_UPGRADE:	sprintf(parameter,"GPS Receiver Upgrade [%d, %d, %d]",
											parameter1, parameter2, parameter3);
										strcat(msg,parameter);								break;
		
		case LOG_TOD_CHG_BEFORE:	
								sprintf(parameter,"TOD before: [%d/%d/%d %d:%d:%d]", 
    								parameter1,
    								(parameter2>>24)&0x000000FF,
    								(parameter2>>16)&0x000000FF,
    								(parameter2>>8)&0x000000FF,
    								(parameter2)&0x000000FF,
    								(parameter3>>24)&0x000000FF
    								);
    							strcat(msg,parameter);
    							break;
    	case LOG_TOD_CHG_AFTER:	
								sprintf(parameter,"TOD after: [%d/%d/%d %d:%d:%d]", 
    								parameter1,
    								(parameter2>>24)&0x000000FF,
    								(parameter2>>16)&0x000000FF,
    								(parameter2>>8)&0x000000FF,
    								(parameter2)&0x000000FF,
    								(parameter3>>24)&0x000000FF
    								);
    							strcat(msg,parameter);
    							break;
    	case LOG_MID_CHG_BEFORE:	
								sprintf(parameter,"EB before: [%d/%d/%d %d:%d:%d]", 
    								parameter1,
    								(parameter2>>24)&0x000000FF,
    								(parameter2>>16)&0x000000FF,
    								(parameter2>>8)&0x000000FF,
    								(parameter2)&0x000000FF,
    								(parameter3>>24)&0x000000FF
    								);
    							strcat(msg,parameter);
    							break;
    	case LOG_MID_CHG_AFTER:	
								sprintf(parameter,"EB after: [%d/%d/%d %d:%d:%d]", 
    								parameter1,
    								(parameter2>>24)&0x000000FF,
    								(parameter2>>16)&0x000000FF,
    								(parameter2>>8)&0x000000FF,
    								(parameter2)&0x000000FF,
    								(parameter3>>24)&0x000000FF
    								);
    							strcat(msg,parameter);
    							break;
		default:
								sprintf(parameter,"undefined log [%d][%d, %d, %d]",
											name,
											parameter1, parameter2, parameter3);
										strcat(msg,parameter);								break;
										
								break;
	}

	sprintf(msg_to_write,"%3d> %04d/%02d/%02d "
						 "%02d:%02d:%02d "
						 "%03d "
						 "%09d "
						 "%s "
						 "%s "
						 "%s\n"
				,LOG.LogCounter, SYSTEM.TIME.CUR.YEAR, SYSTEM.TIME.CUR.MONTH, SYSTEM.TIME.CUR.DAY
				,SYSTEM.TIME.CUR.HOUR, SYSTEM.TIME.CUR.MINUTE, SYSTEM.TIME.CUR.SECOND
				,TimeSliceCounter
				,GC.RunningTime
				,CAT1[cata1].str
				,CAT2[cata2].str
				,msg
			);
	LogWirteToFile(name,msg_to_write);
}

void LogItString(int cata1, int cata2, char *s)
{
	
	LOG.CNT++;
	LOG.LogCounter++;
	
	if( strlen(s) > 700 ) printf("Too long log string:%s\n",s);
	sprintf(msg_to_write,"%3d> %04d/%02d/%02d "
						 "%02d:%02d:%02d "
						 "%03d "
						 "%09d "
						 "%s "
						 "%s "
						 "%s\n"
				,LOG.LogCounter, SYSTEM.TIME.CUR.YEAR, SYSTEM.TIME.CUR.MONTH, SYSTEM.TIME.CUR.DAY
				,SYSTEM.TIME.CUR.HOUR, SYSTEM.TIME.CUR.MINUTE, SYSTEM.TIME.CUR.SECOND
				,TimeSliceCounter
				,GC.RunningTime
				,CAT1[cata1].str
				,CAT2[cata2].str
				,s
			);
	LogWirteToFile2nd(msg_to_write);

}

void LogItString1(int cata1, int cata2, char *s, int v1)
{

	LOG.CNT++;
	LOG.LogCounter++;
	
	if( strlen(s) > 700 ) printf("Too long log string:%s\n",s);
	sprintf(msg_to_write,"%3d> %04d/%02d/%02d "
						 "%02d:%02d:%02d "
						 "%03d "
						 "%09d "
						 "%s "
						 "%s "
						 "%s %d\n"
				,LOG.LogCounter, SYSTEM.TIME.CUR.YEAR, SYSTEM.TIME.CUR.MONTH, SYSTEM.TIME.CUR.DAY
				,SYSTEM.TIME.CUR.HOUR, SYSTEM.TIME.CUR.MINUTE, SYSTEM.TIME.CUR.SECOND
				,TimeSliceCounter
				,GC.RunningTime
				,CAT1[cata1].str
				,CAT2[cata2].str
				,s
				,v1
			);

	LogWirteToFile2nd(msg_to_write);
}

void LogItString2(int cata1, int cata2, char *s, int v1, int v2)
{
	
	LOG.CNT++;
	LOG.LogCounter++;
	
	if( strlen(s) > 700 ) printf("Too long log string:%s\n",s);
	sprintf(msg_to_write,"%3d> %04d/%02d/%02d "
						 "%02d:%02d:%02d "
						 "%03d "
						 "%09d "
						 "%s "
						 "%s "
						 "%s %d %d\n"
				,LOG.LogCounter, SYSTEM.TIME.CUR.YEAR, SYSTEM.TIME.CUR.MONTH, SYSTEM.TIME.CUR.DAY
				,SYSTEM.TIME.CUR.HOUR, SYSTEM.TIME.CUR.MINUTE, SYSTEM.TIME.CUR.SECOND
				,TimeSliceCounter
				,GC.RunningTime
				,CAT1[cata1].str
				,CAT2[cata2].str
				,s
				,v1
				,v2
			);

	LogWirteToFile2nd(msg_to_write);
}

void LogItString3(int cata1, int cata2, char *s, int v1, int v2, int v3)
{
	
	LOG.CNT++;
	LOG.LogCounter++;
	
	if( strlen(s) > 700 ) printf("Too long log string:%s\n",s);
	sprintf(msg_to_write,"%3d> %04d/%02d/%02d "
						 "%02d:%02d:%02d "
						 "%03d "
						 "%09d "
						 "%s "
						 "%s "
						 "%s %d %d %d\n"
				,LOG.LogCounter, SYSTEM.TIME.CUR.YEAR, SYSTEM.TIME.CUR.MONTH, SYSTEM.TIME.CUR.DAY
				,SYSTEM.TIME.CUR.HOUR, SYSTEM.TIME.CUR.MINUTE, SYSTEM.TIME.CUR.SECOND
				,TimeSliceCounter
				,GC.RunningTime
				,CAT1[cata1].str
				,CAT2[cata2].str
				,s
				,v1
				,v2
				,v3
			);

	LogWirteToFile2nd(msg_to_write);

}

void LogItString6(int cata1, int cata2, char *s, unsigned short v1, unsigned char v2, 
	unsigned char v3, unsigned char v4, unsigned char v5, unsigned char v6)
{
	
	LOG.CNT++;
	LOG.LogCounter++;
	
	if( strlen(s) > 700 ) printf("Too long log string:%s\n",s);
	sprintf(msg_to_write,"%3d> %04d/%02d/%02d "
						 "%02d:%02d:%02d "
						 "%03d "
						 "%09d "
						 "%s "
						 "%s "
						 "%s %d %d %d %d %d %d\n"
				,LOG.LogCounter, SYSTEM.TIME.CUR.YEAR, SYSTEM.TIME.CUR.MONTH, SYSTEM.TIME.CUR.DAY
				,SYSTEM.TIME.CUR.HOUR, SYSTEM.TIME.CUR.MINUTE, SYSTEM.TIME.CUR.SECOND
				,TimeSliceCounter
				,GC.RunningTime
				,CAT1[cata1].str
				,CAT2[cata2].str
				,s
				,v1 ,v2, v3, v4, v5 ,v6 
			);

	LogWirteToFile2nd(msg_to_write);

}

